const express = require('express');
const QRCode = require('qrcode');
const { v4: uuidv4 } = require('uuid');
const app = express();


app.use(express.json());

app.post('/qrcode', async(req, res) => { 
    // 获取前端传来的参数
    // const text = req.body.text;

    // 生成唯一的uuid
    const uuid = uuidv4();

try{

    // 生成二维码
    const qrcodeUrl = await QRCode.toDataURL(uuid,{
        errorCorrectionLevel: 'H',
        type: 'image/png'
    });


    res.json({ 
        qrCode:qrcodeUrl,
        uuid,
    });

}catch(err){
    console.log('生成二维码失败',err);
    res.status(500).json({
        message: '生成二维码失败'
    });
}    

});


// 导出 app 实例
module.exports = app;